package com.shuqi.common;

import android.app.Activity;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.shuqi.common.utils.Util;
import com.shuqi.controller.R;
import com.shuqi.database.dao.impl.VersionShowDao;
import com.shuqi.database.dao.impl.VersionShowDao.VersionShowEnum;

/**
 * 
 * @author li.li
 * @date 2014-6-10
 * 
 *       帮助页显示处理
 * 
 *       注：需要在Activity初始化完成后调用（onAttachedToWindow）
 */
public class ViewHelpProvider {
    private static final ViewHelpProvider instance = new ViewHelpProvider();

    /**
     * 
     * 支持帮助页显示的资源类型
     * 
     */
    public enum ViewHelpHandleType {
        pic, // 图片类型
        layout, // 视图类型
        ;
    }

    private ViewHelpProvider() {}

    public static ViewHelpProvider getInstance() {
        return instance;
    }

    public PopupWindow handle(Activity act, final VersionShowEnum nameEnum,
            ViewHelpHandleType handleType, View parentView, int resId) {

        if (ViewHelpHandleType.pic.equals(handleType)) {

            return handleForPic(act, nameEnum, parentView, resId);

        } else if (ViewHelpHandleType.layout.equals(handleType)) {

            return handleForLayout(act, nameEnum, parentView, resId);

        }

        return null;
    }

    /**
     * 执行处理图片类型
     * 
     * @param act
     * @param nameEnum 版本显示功能分类枚举
     * @param parentView 顶级父类View
     * @param dResId 图片资源
     */
    private PopupWindow handleForPic(Activity act, final VersionShowEnum nameEnum, View parentView,
            int dResId) {
        if (act == null || nameEnum == null)
            return null;

        if (isShow(nameEnum)) {
            View view = LayoutInflater.from(act).inflate(R.layout.view_help_common, null);

            Rect appRect = Util.getAppRect(act);

            final PopupWindow bottomPopMenu =
                    new PopupWindow(view, appRect.width(), appRect.height());
            bottomPopMenu.setAnimationStyle(android.R.style.Animation_Dialog);
            bottomPopMenu.showAtLocation(parentView, Gravity.NO_GRAVITY, appRect.left, appRect.top);
            View helpImg = view.findViewById(R.id.view_help_common_img);
            View helpParent = view.findViewById(R.id.view_help_common_parent);

            // 设置背景
            helpParent.setBackgroundResource(dResId);

            // 设置关闭响应事件
            helpImg.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    bottomPopMenu.dismiss();
                    setStatusHide(nameEnum);// 更新数据不再显示
                }
            });


            bottomPopMenu.update();// 显示

            return bottomPopMenu;
        }

        return null;



    }

    /**
     * 执行处理视图类型
     * 
     * 需要在视图里声明 view_help_common_text和view_help_common_parent两个id的使用
     * 
     * @param act
     * @param nameEnum 版本显示功能分类枚举
     * @param parentView 顶级父类View
     * @param layoutResId 视图资源
     */
    private PopupWindow handleForLayout(Activity act, final VersionShowEnum nameEnum,
            View parentView, int layoutResId) {
        if (act == null || nameEnum == null)
            return null;

        if (isShow(nameEnum)) {
            View view = LayoutInflater.from(act).inflate(layoutResId, null);

            Rect appRect = Util.getAppRect(act);

            int height = ViewGroup.LayoutParams.FILL_PARENT;
            
            final PopupWindow bottomPopMenu = new PopupWindow(view, appRect.width(), height, true);
            bottomPopMenu.setAnimationStyle(android.R.style.Animation);
            bottomPopMenu.showAtLocation(parentView, Gravity.NO_GRAVITY, appRect.left, 0);
            View helpImg = view.findViewById(R.id.view_help_common_img);
            View helpParent = view.findViewById(R.id.view_help_common_parent);

            // 屏蔽后退按钮
            helpParent.setOnKeyListener(new View.OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    return true;
                }
            });

            // 监听点击，退出
            helpImg.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    bottomPopMenu.dismiss();
                    setStatusHide(nameEnum);// 更新数据不再显示
                }
            });


            bottomPopMenu.update();// 显示

            return bottomPopMenu;
        }

        return null;



    }

    /**
     * 是否显示
     * 
     * @param nameEnum
     * @return false 不显示, true 显示
     */
    public boolean isShow(VersionShowEnum nameEnum) {
        return VersionShowDao.getInstance().isShow(nameEnum);



    }

    /**
     * 键home键
     * 
     * @param act
     */
    public void lockHomeKey(Activity act) {
        int version = android.os.Build.VERSION.SDK_INT;
        if (version >= 15) {
            act.getWindow().setFlags(0x80000000, 0x80000000);
        } else {
            act.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        }
    }

    /**
     * 设置不显示
     * 
     * @param nameEnum
     */
    private void setStatusHide(VersionShowEnum nameEnum) {
        VersionShowDao.getInstance().update(nameEnum);
    }

}
